//
//  EmotionModel.swift
//  Emotion
//
//  Created by maker on 2016/11/21.
//  Copyright © 2016年 maker. All rights reserved.
//

import UIKit

class EmotionModel: NSObject ,NSCoding{
    var code :String?{ //emoji 对应的支付
        didSet{
            guard let code = code else {
                return
            }
            //1.创建扫描器
            let scanner = Scanner(string: code)
            
            //2.扫描 code 中的值
            var value : uint = 0
            scanner.scanHexInt32(&value)
            
            //3.将 value 转成字符
            let c = Character(UnicodeScalar(value)!)
            
            //4.将字符转成 字符串
            codeString = String(c)
        }
    }
    var chs :String?    //表情对应的汉字
    var png :String?{   //表情对应的图片
        didSet{
            guard let png = png else {
                return
            }
            pngPath = Bundle.main.bundlePath + "/Emoticons.bundle/" + png
        }
    }
    
    //计算属性
    var pngPath : String?   //可用的图片路径
    var codeString :String? //code 转成的字符串
    var isDelete :Bool = false //记录是否删除按钮
    var isEmpty : Bool = false //是否为空白表情
    
    
    init(dict : [String : AnyObject]) {
        super.init()
        setValuesForKeys(dict)
    }
    init(isDelete : Bool) {
        super.init()
        self.isDelete = isDelete
    }
    init(isEmpty : Bool) {
        super.init()
        self.isEmpty = isEmpty
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        
    }
    override var description: String{
        return dictionaryWithValues(forKeys: ["codeString","chs","pngPath"]).description
    }
    
    //解归档
     required init?(coder aDecoder: NSCoder) {
        codeString = aDecoder.decodeObject(forKey: "codeString") as? String
        chs = aDecoder.decodeObject(forKey: "chs") as? String
        pngPath = aDecoder.decodeObject(forKey: "pngPath") as? String
    }
    
    func encode(with aCoder: NSCoder) {
        aCoder.encode(codeString, forKey: "codeString")
        aCoder.encode(chs, forKey: "chs")
        aCoder.encode(pngPath, forKey: "pngPath")
    }
    
}
